package com.huangpuweb.api.util;

import com.alibaba.fastjson.JSONObject;
import com.huangpuweb.api.model.SysEventDataAll;
import com.huangpuweb.api.service.CrowdSituationApiService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

@Component
@Lazy(value = false)
public class ScheduledTask {
    public static Map<String, String> map = new ConcurrentHashMap<>();
    @Autowired
    private CrowdSituationApiService crowdSituationApiService;

//        @Scheduled(cron = "0/30 * * * * ?")
    //关闭华为垃圾时间推送
  /*  @Scheduled(cron = "0 0 8 * * ?")*/
//    public void eventBasePush() {
//        List<SysEventDataAll> sysEventData = new ArrayList<>();
////        if (map != null && map.get("eventTime") != null) {
////            sysEventData = crowdSituationApiService.findAllEventInfo(map.get("eventTime"));
////            if (sysEventData.size() == 0) {
////                return;
////            }
////            map.remove("eventTime");
////        } else {
////            sysEventData = crowdSituationApiService.findEventInfo();
////        }
//        //临时演示数据
////        List<SysEventDataAll> test=new ArrayList<>();
//        List<String > strings=new ArrayList<>();
//        strings.add("175");
//        strings.add("231");
//        for (String ss:strings) {
//            SysEventDataAll sysEvent = crowdSituationApiService.findEventInfoById(ss);
//            sysEventData.add(sysEvent);
//        }
//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
////        map.put("eventTime", sysEventData.get(0).getEventtime());
//        //部署173.82.57.27,,,本机172.82.4.74,,,http://192.168.12.10:1080/api/ai/eventbase
//        try {
//            for (SysEventDataAll sysEventDataAll : sysEventData) {
//                Thread.sleep(3000);
//                HttpPost httpPost = new HttpPost("http://173.82.57.27:1080/api/ai/eventbase");
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                sysEventDataAll.setHappentime(simpleDateFormat.parse(sysEventDataAll.getEventtime()).getTime() / 1000);
//                httpPost.setHeader("Content-type", "application/json;charset=utf-8");
//                httpPost.setHeader("X-HW-ID", "ksh.hw.zhhp.gov");
//                httpPost.setHeader("X-HW-APPKEY", "UKD09IO1Ww2P0SfM8IBPKg==");
//                StringEntity stringEntity = new StringEntity(StringUtils.strip(JSONObject.toJSONString(sysEventDataAll), "[]"), "utf-8");
//                stringEntity.setContentType("application/json");
//                httpPost.setEntity(stringEntity);
//                CloseableHttpResponse reponse = httpClient.execute(httpPost);
//                if (reponse.getEntity() != null) {
//                    LogFactory.getLog(this.getClass()).info("华为垃圾事件推送status" + reponse.getStatusLine().getStatusCode());
//                }else {
//                    LogFactory.getLog(this.getClass()).info("华为垃圾事件推送失败" + reponse.getStatusLine().getStatusCode());
//
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
